/*
    13.Write a program in C to extract a substring from a given string. 

    Test Data :
    Input the string : this is test string
    Input the position to start extraction :9
    Input the length of substring :4

    Expected Output :

    The substring retrieve from the string is : " test " 
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STR 1024

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p = ch;
        p++;
    }
    *p = '\0';
    return str;
}

char *getsubstr(char *str, int start, int len)
{
    char *p = malloc(MAX_STR);
    strncpy(p, (str + start - 1), len);
    *(p + len) = '\0';
    return p;
}

int main(void)
{
    char *str = malloc(MAX_STR);
    int pos,len;
    printf("Input the string : ");
    getstr(str);
    printf("Input the position to start extraction : ");
    scanf("%d", &pos);
    printf("Input the length of substring : ");
    scanf("%d", &len);
    printf("The substring retrieve from the string is : \" %s \"", getsubstr(str, pos, len));

    return 0;
}